<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\ChangeStatus;
use App\Admin\Repositories\PopularActivity;
use App\Models\PopularActivity as ModelsPopularActivity;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class PopularActivityController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new PopularActivity(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('description');
            $grid->column('tag');
            $grid->column('cover')->image('', 120, 60);
            $grid->column('time_start');
            $grid->column('time_end');
            $grid->column('order')->badge()->editable(true);
            $grid->column('status')
                ->using([0 => '无效', 1 => '有效'])
                ->label([0 => 'gray', 1 => 'green']);
            $grid->column('updated_at')->sortable();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->prepend(new ChangeStatus(ModelsPopularActivity::class, '有效/失效'));
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->scope('trashed', '回收站')->onlyTrahsed();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new PopularActivity(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('description');
            $show->field('tag')->explode()->label();
            $show->field('cover')->image();
            $show->field('images')->image();
            $show->field('content')->unescape();
            $show->field('time_start');
            $show->field('time_end');
            $show->field('address');
            $show->field('organizers');
            $show->field('participate');
            $show->field('order')->badge();
            $show->field('status')->using([
                0 => '无效',
                1 => '有效'
            ])->label('green');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new PopularActivity(), function (Form $form) {

            $form->display('id');
            $form->text('title');
            $form->textarea('description');
            $form->image('cover')->saveFullUrl()
                ->autoUpload()->accept('jpg,png,gif,jpeg');
            $form->multipleImage('images')->saveFullUrl()
                ->autoUpload()->accept('jpg,png,gif,jpeg')
                ->saving(function ($value) {
                    return implode(',', $value);
                });
            $form->editor('content');

            $form->datetimeRange('time_start', 'time_end', '活动时间');
            $form->text('address');
            $form->text('organizers');
            $form->text('participate');
            $form->tags('tag')->help('标签输入完成后要以逗号结尾')
                ->saving(function ($v) {
                    return implode(',', $v);
                });
            $form->number('order')->default(1);
            $form->radio('status')->options([0 => '无效', 1 => '有效'])->default(1);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
